package com.light.roller;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.buffer.BufferObjectManager;

import android.content.Intent;
import android.content.res.Configuration;
import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.huw.NewBie.ParticleEffect1;
import com.huw.NewBie.ParticleEffect2;
import com.huw.NewBie.ParticleEffect3;
import com.huw.debuger.Debugger;

public class Roller_End extends Roller{
	
	ParticleEffect1 effect1;
	ParticleEffect1 effect2;
	ParticleEffect1 effect3;
	ParticleEffect1 effect4;
	ParticleEffect1 effect5;

	
	ParticleEffect2 effect2_1;
	ParticleEffect2 effect2_2;
	
	ParticleEffect3 effect3_1;
	
	Sprite WhitePot;
	
	
	@Override
	public Engine onLoadEngine() {

		Vector2 cameraCenter = new Vector2( 1608, 317);
		Vector2 playerInitialPositon = cameraCenter.cpy();
		Vector2 doorPosition = new Vector2(530,370);
		stage = new Stage_End(this);
		stage.onLoadEngine();
		Engine engine = initialize(cameraCenter, playerInitialPositon,doorPosition,Direction.UP);
		Debugger.HIDE_STATIC_WALL = false;
		
		effect1 = new ParticleEffect1(this);
		effect2 = new ParticleEffect1(this);
		effect3 = new ParticleEffect1(this);
		effect4 = new ParticleEffect1(this);
		effect5 = new ParticleEffect1(this);

		effect2_1 = new ParticleEffect2(this);
		effect2_2 = new ParticleEffect2(this);
		effect3_1 = new ParticleEffect3(this);
		return engine;
	}
	
	public void reset()
	{
		ResourceManager.unLoadAllSounds();
		stage.removeStoper(scene, physicsWorld);
		Roller_End.this.finish();
		Roller_End.this.startActivity(new Intent(Roller_End.this, Roller_End.class));
		BufferObjectManager.getActiveInstance().clear();
		
	}
	
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		effect1.onLoadResources();
		effect2.onLoadResources();
		effect3.onLoadResources();
		effect4.onLoadResources();
		effect5.onLoadResources();
		effect2_1.onLoadResources();
		effect2_2.onLoadResources();
		effect3_1.onLoadResources();
		
		ResourceManager.onLoadResources("particle_small_point", this);
	}
	
	
	@Override
	protected void nextStageSetting()
	{
		Roller_End.this.finish();
		Roller_End.this.startActivity(new Intent(Roller_End.this, Credits.class));
		BufferObjectManager.getActiveInstance().clear();
		
	}
	
	float timeFactor = 1.0f;
	
	public float getTimeFactor()
	{
		return timeFactor;
	}
	
	public void setTimeFactor(float factor)
	{
		timeFactor = factor;
	}
	
	enum EndingState 
	{
		playerControl,
		moving,
		slowMotionFlying,
		hangOnAir,		
		blacking,
	}
	
	
	Roller_End m_game = this;
	
			
	@Override
	public Scene onLoadScene() 
	{
		Scene tempScene;		
		
		tempScene = super.onLoadScene();
		
		scene.unregisterUpdateHandler(physicsWorld);
 
		effect1.onLoadScene(tempScene);
		effect2.onLoadScene(tempScene);
		effect3.onLoadScene(tempScene);
		effect4.onLoadScene(tempScene);
		effect5.onLoadScene(tempScene);
		effect2_1.onLoadScene(tempScene);
		effect2_2.onLoadScene(tempScene);
		effect3_1.onLoadScene(tempScene);
		
		WhitePot = new Sprite(0, 0, ResourceManager.getTextureRegion("particle_small_point"));
		WhitePot.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		WhitePot.setColor(1, 1, 1);
		

		IUpdateHandler physicWorldHandler = new IUpdateHandler()
		{
			EndingState endingState = EndingState.playerControl;
			float FlyingDelay = 0.4f;
			float boom = 11f;
			float finishCounter = 0f;
			final float AnimationPosition = 1474f;
			final float JumpPosition = 1118f;
			final float HangOnPositionY = 480f;
			final float SlowMotionFactor = 0.3f;
			final float NormalMotionFactor = 1f;
			final float FinishDelay = 3f;
			final float BlakingDuration = 2f;
			
			
			
			
			@Override
			public void onUpdate(float pSecondsElapsed) 
			{				
				physicsWorld.onUpdate(pSecondsElapsed*getTimeFactor());						
				
				switch(endingState)
				{
				case playerControl:					
					if(player.getX()<AnimationPosition)
					{
						animateInit();
						endingState = EndingState.moving;							
					}					
					break;			
				case moving:
					if(player.getX()<JumpPosition)
					{						
						player.jump(nowDirection);
						endingState = EndingState.slowMotionFlying;
					}					
					break;
				case slowMotionFlying:
					
					FlyingDelay -= pSecondsElapsed;
					
					if(FlyingDelay<0)
					{
						setTimeFactor(SlowMotionFactor);
					}					
					
					if(player.getY()>HangOnPositionY)
					{			
						physicsWorld.setGravity(new Vector2(0, 0));
						player.getBody().setLinearVelocity(new Vector2(0,0));
						player.leftClicked = false;
						player.hideLight();
						endingState = EndingState.hangOnAir;						
					}					
					
					break;
				case hangOnAir:					
					boom -= pSecondsElapsed;
					int scaleFactor = 220;
					
					if(boom<-3f&&WhitePot.getScaleX()==scaleFactor)
					{
									
						finishCounter+=pSecondsElapsed;
						
						if(finishCounter>FinishDelay)
						{		
							cameraManager.CameraLightOff(BlakingDuration);
							endingState = EndingState.blacking;
						}
								
					}
					else if(boom<-3f)
					{
						player.kill();
						physicsWorld.setGravity(new Vector2(0, SensorManager.GRAVITY_EARTH));
						
						WhitePot.setPosition(player.getX()+15,player.getY()+25);
						scene.attachChild(WhitePot);
						ScaleModifier scaleModifier;
						scaleModifier = new ScaleModifier(1, 2.5f, scaleFactor);
						WhitePot.registerEntityModifier(scaleModifier);					
						
					}
					else if(boom<1f)
					{
						effect3_1.shoot(new Vector2(player.getX()+15,player.getY()+25));
					}
					else if(boom<2.5f)
					{
						effect2_2.shoot(new Vector2(player.getX()+15,player.getY()+25));
					}
					else if(boom<4.0f)
					{
						effect2_1.shoot(new Vector2(player.getX()+15,player.getY()+25));
					}
					else if(boom<5.2f)
					{						
						effect5.shoot(new Vector2(player.getX(),player.getY()));
					}
					else if(boom < 5.5f)	
					{
						effect4.shoot(new Vector2(player.getX()+30,player.getY()+80));
					}
					else if(boom < 5.8f)	
					{				
						effect3.shoot(new Vector2(player.getX()+70,player.getY()+60));
					}
					else if(boom < 7.5f)	
					{
						effect2.shoot(new Vector2(player.getX()+50,player.getY()+40));
					}
					else if(boom < 9.2f)	
					{
						effect1.shoot(new Vector2(player.getX()+40,player.getY()+20));
					}
					
					
					break;
					
				case blacking:			
					
					
					if(cameraManager.isLightOff())
					{					
						nextStageSetting();
					}				
					
					break;
				
				}
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			

			
			void animateInit()
			{
				hideButton();				
				physicsWorld.setGravity(new Vector2(0, SensorManager.GRAVITY_EARTH));
				nowDirection = Direction.UP;
				
				
				if (Roller.orientation == Configuration.ORIENTATION_PORTRAIT)
				{
				sensorEvent.values[0]=(float) (Roller.GSENSOR_SENSITIVITY+1);
				sensorEvent.values[1]=0;
				}
				else
				{
				sensorEvent.values[0]=0;
				sensorEvent.values[1]=(float) (Roller.GSENSOR_SENSITIVITY+1);
				}
				
				player.getBody().setLinearVelocity(new Vector2(0,0));
				player.leftClicked = true;
				
				changeDirection();
			}
			
		};
		
		this.player.AutoReset = false;		
		scene.registerUpdateHandler(physicWorldHandler);		
			
		return tempScene;
	}
	
	


}
